package com.aaa.utils;

import com.aaa.entity.DataBackup;
import com.aaa.mapper.DataBackupMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MysqlBackup {

    @Resource
    private DataBackupMapper dataBackupMapper;

    @Value("${backup.windows.url}")
    private String windowsUrl;

    @Value("${backup.linux.url}")
    private String linuxUrl;

    @Value("${backup.datasource.username}")
    private String username;

    @Value("${backup.datasource.password}")
    private String password;

    @Value("${backup.datasource.dbname}")
    private String dbName;


    public Process linuxEnv(String command) {
        Process ps = null;
        try {
            String[] commands = {"/bin/sh", "-c", command};
            ps = Runtime.getRuntime().exec(commands);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ps;
    }

    public Process windowsEnv(String command) {
        Process ps = null;
        try {
            String[] commands = {"cmd", "/c", command};
            ps = Runtime.getRuntime().exec(commands);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ps;
    }

    public boolean dbBackup(String createUser) {
        String dirPath;
        if (System.getProperty("os.name").toLowerCase().contains("windows")) {
            dirPath = windowsUrl;
        } else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
            dirPath = linuxUrl;
        }else {
            return false;
        }
        File backupDir = new File(dirPath);
        if (!backupDir.exists()) {
            backupDir.mkdirs();
        }
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            String dateStr = simpleDateFormat.format(date)+"进行了数据备份";
            File sqlFile = new File(backupDir, new SimpleDateFormat("yyyyMMddHHmmss").format(date)+".sql");
            if (!sqlFile.exists()) {
                sqlFile.createNewFile();
            }
            //mysqldump -h localhost -u root -p db_name > db_name.sql
            StringBuffer buffer = new StringBuffer();
            buffer.append("mysqldump");
            buffer.append(" -h 127.0.0.1");
            buffer.append(" -u").append(username);
            buffer.append(" -p").append(password);
            buffer.append(" ").append(dbName).append(" >");
            buffer.append(sqlFile);
//            System.out.println("cmd命令为：" + buffer.toString());
            Runtime runtime = Runtime.getRuntime();
//            System.out.println("开始备份：" + dbName);
            if (System.getProperty("os.name").toLowerCase().contains("windows")) {
                windowsEnv(buffer.toString());
            } else if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                linuxEnv(buffer.toString());
            } else {
                return false;
            }
            System.out.println(sqlFile.getPath());
            System.out.println("备份成功!");
            DataBackup dataBackup = new DataBackup();
            dataBackup.setOperateDate(date);
            dataBackup.setOperateDetails(dateStr);
            dataBackup.setOperateUser(createUser);
            dataBackup.setFileUrl(sqlFile.getPath());
            dataBackupMapper.addDataBackup(dataBackup);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

//    public static void main(String[] args) {
//        try {
//            String backName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".sql";
//            new MysqlBackup().dbBackup("root","hm990917","hospitaldata",backName);
//        }catch (Exception ex){
//            ex.printStackTrace();
//        }
//    }


}
